var mainxunResult;

var prefixTF = 'av_';

var xunShedulingxunResult = {};

function ra(v) {
	return Math.random() * v;
}

var xunActiveSheet = 17;

var xunxunResultSheet = [
	//	{id: 1, name: '全流域', type:1, sec:0},
	{ id: 2, name: '行政区', type: 1, sec: 1 },
	/*{id: 3, name: '宜春市', type:2, sec:2},
	{id: 4, name: '新余市', type:2, sec:2},
	{id: 5, name: '萍乡市', type:2, sec:2},
	{id: 6, name: '南昌市', type:2, sec:2},
	{id: 7, name: '吉安市', type:2, sec:2},
	{id: 8, name: '赣州市', type:2, sec:2},
	{id: 9, name: '抚州市', type:2, sec:2},*/
	{ id: 10, name: '水资源区', type: 1, sec: 3 },
	/*{id: 11, name: '赣江上游-万栋', type:2, sec:4},
	{id: 12, name: '赣江上游-万上', type:2, sec:4},
	{id: 13, name: '赣江中游-栋石', type:2, sec:4},
	{id: 14, name: '赣江中游-石峡', type:2, sec:4},
	{id: 15, name: '赣江下游', type:2, sec:4},
	{id: 16, name: '鄱阳湖环湖', type:2, sec:4},*/
	{ id: 17, name: '►用水单元', type: 1, sec: 5 },
	/*{ id: 18, name: '断面', type: 1, sec: 6 },
	{ id: 19, name: '沙子岭', type: 2, sec: 7, branch: 1 },
	{ id: 20, name: '南丰', type: 2, sec: 7, branch: 2 },
	{ id: 21, name: '南城', type: 2, sec: 7, branch: 3 },
	{ id: 22, name: '黎川', type: 2, sec: 7, branch: 4 },
	{ id: 23, name: '廖家湾', type: 2, sec: 7, branch: 5 },
	{ id: 24, name: '娄家村', type: 2, sec: 7, branch: 6 },
	{ id: 25, name: '焦石', type: 2, sec: 7, branch: 7 },
	{ id: 26, name: '李家渡', type: 2, sec: 7, branch: 8 },*/
	{ id: 27, name: '水库', type: 1, sec: 8 },
	{ id: 28, name: '洪门水库', type: 2, sec: 9, branch: 1 },
	{ id: 29, name: '廖坊水库', type: 2, sec: 9, branch: 2 }
];

function xunxunResultViewChange(id) {
	var selectedItem;
	for (var i = 0; i < xunxunResultSheet.length; i++) {
		if (xunxunResultSheet[i].id == id) {
			selectedItem = xunxunResultSheet[i];
		}
	}
	if (xunActiveSheet > 0) {
		var old = document.getElementById('xunResult' + xunActiveSheet);
		old.innerHTML = old.innerHTML.replace('►', '');
	}
	var tab = document.getElementById('xunResult' + id);
	tab.innerHTML = '►' + tab.innerHTML;
	xunActiveSheet = id;
	Ext.getCmp('schedulingxunResult').setActiveItem(selectedItem.sec);

	if(selectedItem.sec == 7){
		changeSectionSingleView(selectedItem.branch);
	}
	else if(selectedItem.sec == 9){
		xunChangeReserviorSingleView(selectedItem.branch);
	}
}

function xunGetxunxunResultSheet() {
	var output = '<div style="background-color:#ddebf7;width:100%;height:100%;border: 1px solid #ddebf7;padding: 10px 10px 10px 10px;margin: 5px 0 0 0;">';
	for (var i = 0; i < xunxunResultSheet.length; i++) {
		if (xunxunResultSheet[i].type == 1) output += '<div id="xunResult' + xunxunResultSheet[i].id + '" style="font-weight:bold;font-size:16px;margin:10px 0 10px 0;border-bottom: 1px solid black;cursor: pointer;" onclick="xunxunResultViewChange(' + xunxunResultSheet[i].id + ')">' + xunxunResultSheet[i].name + '</div>';
		else output += '<div id="xunResult' + xunxunResultSheet[i].id + '" style="text-align: right;font-size:14px;margin:10px 0 10px 0;cursor: pointer;" onclick="xunxunResultViewChange(' + xunxunResultSheet[i].id + ')">' + xunxunResultSheet[i].name + '</div>';
	}
	output += '</div>';
	return output;
}

function xunGetDataArray(store, index, start, end) {
	var output = new Array();
	var t = 0;
	for (var i = start; i < end; i++) {
		output[t] = store.getAt(i).get(index);
		t++;
	}
	return output;
}

function xunStartCalculation(){
	var link = "szy/dispatch/wateroptimum/xunSchedulingCalculation";

	var p = Ext.create('Ext.ProgressBar', {});

	var xunWin = Ext.create('Ext.window.Window', {
	    title: '请稍候...',
	    height: 140,
	    width: 400,
	    closable: false,
	    modal : true,
	    resizable: false,
	    layout: 'border',
    	padding:'5 15 20 15',
    	border: false,
	    items: [{
	    	xtype:'panel',
	    	region:'north',
	    	html:'<span id="xuntip" style="line-height:38px;font-size:16px;">正在查询水库信息...</span>',
	    	height: 45,
	    	border: false
	    },{
	    	xtype:'panel',
	    	region:'center',
	    	layout: 'fit',
	    	border: false,
	    	items: p
	    }],
	    listeners: {
			show: {
				fn: function(){

				}
			}
		}
	}).show();

	var task = {
		run: function() {
			if(p.getValue() < 0.9){
				p.updateProgress(p.getValue() + 0.025);
				if(p.getValue() >= 0.5)$("#xuntip").html('正在计算中...');
				else if(p.getValue() >= 0.2)$("#xuntip").html('正在加载供需水信息...');
			}
			else return false;
		},
		interval: 40,
		repeat: 36
	};

	Ext.TaskManager.start(task);
	
	$.ajax({
	//	url: "http://localhost:8080/Springwap/"+link+".shtml",

		url: config.URL+link,
		type: "post",
		data: {},
		dataType: "json",
		success: function(xunResult) {
			xunShedulingxunResult = xunResult;
			mainxunResult = xunShedulingxunResult;
			var taskFull = {
				run: function() {
					if(p.getValue() >= 0.9){
						p.updateProgress(p.getValue() + 0.01);
						if(p.getValue() >= 1){
							xunFillDataSchedulingPlan(xunResult.index1);
							xunFillDataAdministrativeArea(xunResult.index2);
							xunFillDataWaterArea(xunResult.index3);
							loadxunWaterUnitData2(xunResult.index4);
							xunFillDataReservior(xunResult.index6);
							xunFillDataReserviorSingle(xunResult.index8);
							xunWin.close();
							Ext.getCmp('xunMainTab').setActiveItem(2);
							return false;
						}
						else if(p.getValue() >= 0.9)$("#xuntip").html('计算完毕，正在处理显示数据...');
					}
				},
				interval: 25
			};
			Ext.TaskManager.start(taskFull);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			xunWin.close();
			Ext.Msg.alert('提示', '计算时出现了错误！');
		}
	});
}


function customRound(value, record){
	if(value == null)return null;
	return Ext.util.Format.round(value, 3);
}

function customRound2(value, record){
	if(value == null)return null;
	return Ext.util.Format.round(value, 2).toFixed(2);
}

Ext.define('Admin.view.needwater.waterOptimumxun', {
	extend: 'Ext.container.Container',
	xtype: 'waterOptimumxun',
	alias: 'waterOptimumxun',
//	controller: 'waterOptimumxunController',
	user: [],
	userCLs: 'big-100 small-100',
	layout: 'fit',
	items: [{
		xtype: 'tabpanel', // TabPanel itself has no title
		activeTab: 0,      // First tab active by default
		id: 'xunMainTab',
		items: [{
			title: '初始条件',
			xtype: 'xunInitialConditions'
		//	xtype: 'xunAllBasin'
		}, {
			title: '调节方式',
			xtype: 'panel',
			layout: 'fit',
			border: false,
			items: [{
				layout: 'card',
				region: 'center',
				border: false,
				id: 'xunSchedulingCard',
				items: [{
					xtype: 'xunSchedulingChart'
				}, {
					xtype: 'xunSchedulingConstraints'
				}, {
					html: "Third Item"
				}],
				dockedItems: [{
					xtype: 'toolbar',
					dock: 'top',
					height: 50,
					items: [{
						xtype: 'button',
						html: '<span id="xunSCbtn0" style="color:#000000;font-weight:bold;">►调度图</span>',
						style: 'width:120px;height:30px;background-color:#ddebf7;',
						margin: '0 20 0 10',
						handler: function () {
							Ext.getCmp('xunSchedulingCard').setActiveItem(0);
							document.getElementById("xunSCbtn1").innerHTML = document.getElementById("xunSCbtn1").innerHTML.replace('►', '');
							document.getElementById("xunSCbtn1").style.fontWeight = 'normal';
							document.getElementById("xunSCbtn0").innerHTML = document.getElementById("xunSCbtn0").innerHTML.replace('►', '');
							document.getElementById("xunSCbtn0").innerHTML = '►' + document.getElementById("xunSCbtn0").innerHTML;
							document.getElementById("xunSCbtn0").style.fontWeight = 'bold';
						}
					}, {
						xtype: 'button',
						html: '<span id="xunSCbtn1" style="color:#000000;">调度约束</span>',
						style: 'width:120px;height:30px;background-color:#ddebf7;',
						margin: '0 20 0 0',
						handler: function () {
							Ext.getCmp('xunSchedulingCard').setActiveItem(1);
							document.getElementById("xunSCbtn0").innerHTML = document.getElementById("xunSCbtn0").innerHTML.replace('►', '');
							document.getElementById("xunSCbtn0").style.fontWeight = 'normal';
							document.getElementById("xunSCbtn1").innerHTML = document.getElementById("xunSCbtn1").innerHTML.replace('►', '');
							document.getElementById("xunSCbtn1").innerHTML = '►' + document.getElementById("xunSCbtn1").innerHTML;
							document.getElementById("xunSCbtn1").style.fontWeight = 'bold';
						}
					}, '->', {
						xtype: 'button',
						hidden: false,
						html: '<span style="color:#FFFFFF;">调节计算</span>',
						style: 'width:120px;height:30px;background-color:#2c9eee;align:right;',
						margin: '0 20 0 0',
						handler: function () {
							xunStartCalculation();
						}
					}
					]
				}]
			}]
		}, {
			title: '调度方案',
			xtype: 'xunSchedulingPlan'
		}, {
			title: '结果分析',
			layout: {
				type: 'hbox',
				align: 'stretch'
			},
			border: false,
			items: [{
				xtype: 'panel',
				border: false,
				width: 140,
				margin: '10 10 0 15',
			//	padding: '10 10 10 10',
				html: xunGetxunxunResultSheet(),
				style: 'backgroundColor: #ddebf7;'
			}, {
				xtype: 'panel',
				margin: '0 10 0 0',
				layout: 'card',
				id: 'schedulingxunResult',
				header: false,
				title: false,
				margin: '15 10 15 5',
				activeItem: 5,
				items: [{
					html: '0 sec'
				}, {
					xtype: 'xunAdministrativeArea'
				}, {
					html: '2 sec'
				}, {
					xtype: 'xunWaterArea'
				}, {
					html: '4 sec'
				}, {
					xtype: 'xunUseWaterUnit'
				}, {
					html: '4 sec'
				}, {
					html: '4 sec'
				}, {
					xtype: 'xunReservior'
				}, {
					xtype: 'xunReserviorSingle'
				}],
				flex: 1
			}]
		}]
	}],
	listeners: {
		show: function () {
			/*$('#needWaterCal').off('click');
            $('#needWaterCal').on('click', function () {
                xunStartCalculation();
            }); */  	
		}
	}
})